﻿Imports TransportadoraToledoBLL
Imports System.Windows.Forms

Public Class frm_cadastro_combustiveis

#Region "Métodos e Variáveis"
    Public objBLL As New CombustivelBLL
    Public controle As String
    Public ultimo, primeiro, atual As Integer

    Sub carregaPrimeiroUltimo()
        Try
            ultimo = objBLL.localizarPrimeiroUltimo("ultimo")
            primeiro = objBLL.localizarPrimeiroUltimo("primeiro")
            atual = ultimo
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try

    End Sub


    Sub carregaUltimo()
        If (ultimo <> 0) Then
            objBLL.localizar(ultimo)
            If (objBLL.pCodigo <> 0) Then
                carregaCampos()

            End If
        End If
    End Sub
    Sub carregaPrimeiro()
        If (primeiro <> 0) Then
            objBLL.localizar(primeiro)
            If (objBLL.pCodigo <> 0) Then
                carregaCampos()

            End If
        End If
    End Sub

    Sub limpar()
        tbCodigo.Text = ""
        tbtipo.Text = ""
        controle = ""
        objBLL.limpar()
    End Sub

    Sub status(ByVal sta As Boolean)

        If (tbCodigo.Text = "" Or controle = "A") Then
            bIncluir.Enabled = sta

            bExcluir.Enabled = sta
            If (tbCodigo.Text = "") Then
                bAlterar.Enabled = False
                bVaPara.Enabled = False
                bPesquisar.Enabled = False
                bExcluir.Enabled = False
            Else
                bExcluir.Enabled = sta
                bPesquisar.Enabled = sta
                bAlterar.Enabled = sta
                bVaPara.Enabled = sta
            End If
            bCancelar.Enabled = Not sta
            bSalvar.Enabled = Not sta
            bPrimeiro.Enabled = sta
            bProximo.Enabled = sta
            bUltimo.Enabled = sta
            bAnterior.Enabled = sta
            gbConteudo.Enabled = Not sta
        Else
            bIncluir.Enabled = sta

            If (tbCodigo.Text = "") Then
                bExcluir.Enabled = False
                bAlterar.Enabled = False
                bVaPara.Enabled = False
                bPesquisar.Enabled = False
            Else
                bExcluir.Enabled = sta
                bAlterar.Enabled = sta
                bVaPara.Enabled = sta
                bPesquisar.Enabled = sta
            End If

            bCancelar.Enabled = Not sta
            bSalvar.Enabled = Not sta

            gbConteudo.Enabled = Not sta
        End If

        If (atual = primeiro And atual = ultimo) Then
            bProximo.Enabled = False
            bUltimo.Enabled = False
            bAnterior.Enabled = False
            bPrimeiro.Enabled = False
        ElseIf (atual = ultimo) Then
            bProximo.Enabled = False
            bUltimo.Enabled = False
            bAnterior.Enabled = True
            bPrimeiro.Enabled = True
        ElseIf (atual = primeiro) Then
            bProximo.Enabled = True
            bUltimo.Enabled = True
            bAnterior.Enabled = False
            bPrimeiro.Enabled = False
        Else
            bProximo.Enabled = True
            bUltimo.Enabled = True
            bAnterior.Enabled = True
            bPrimeiro.Enabled = True

        End If
    End Sub
    Sub carregaPropriedades()

        Try
            If (tbCodigo.Text = "") Then
                objBLL.pCodigo = 0
            Else
                objBLL.pCodigo = tbCodigo.Text
            End If

            objBLL.ptipo = tbtipo.Text

        Catch ex As Exception
            Throw ex
        End Try
    End Sub

    Sub carregaCampos()
        Try
            tbCodigo.Text = objBLL.pCodigo
            tbtipo.Text = objBLL.ptipo
        Catch ex As Exception
            Throw ex
        End Try
    End Sub

    Public Sub atualizaGrid()
        Try
            dgDados.DataSource = objBLL.localizar(tbConsultar.Text)
            dgDados.Columns(0).HeaderText = "Código"
            dgDados.Columns(1).HeaderText = "Tipo"
            dgDados.Columns(0).Width = 100
            dgDados.Columns(1).Width = 330


        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub
#End Region
    Private Sub frm_cadastro_combustiveis_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.Location = New System.Drawing.Point(Me.Location.X, 212)
        Me.Size = New Size(734, 304)
        carregaPrimeiroUltimo()
        carregaUltimo()
        bCancelar_Click(sender, e)
    End Sub

    Private Sub bIncluir_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bIncluir.Click
        limpar()

        controle = "I"
        status(False)
    End Sub

    Private Sub bExcluir_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bExcluir.Click
        Try
            If (MessageBox.Show("Deseja excluir?", "", MessageBoxButtons.YesNo) = DialogResult.Yes) Then
                carregaPropriedades()
                objBLL.excluir()
                MessageBox.Show("Excluido com sucesso!")
                limpar()
                carregaPrimeiroUltimo()
                carregaUltimo()
                status(True)
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub

    Private Sub bAjuda_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bAjuda.Click
        Dim Ajuda As String
        Ajuda = Application.StartupPath
        Ajuda = Ajuda.Replace("\bin\Debug", "")
        Ajuda = Ajuda & "\Arquivo de Ajuda\index.html"
        Dim Ie As Object
        Ie = CreateObject("InternetExplorer.Application")

        Ie.Visible = True
        Ie.Navigate(Ajuda)
    End Sub


    Private Sub bCancelar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bCancelar.Click
        If (controle = "I") Then
            carregaPrimeiroUltimo()
            carregaUltimo()
        ElseIf (controle = "A") Then
            objBLL.localizar(atual)
            If (objBLL.pCodigo <> 0) Then
                carregaCampos()
                atual = objBLL.pCodigo
            End If
        End If
        controle = ""

        status(True)
    End Sub


    Private Sub bAlterar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bAlterar.Click
        controle = "A"

        status(False)
    End Sub

    Private Sub bVaPara_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bVaPara.Click
        Dim frm As New frm_VaPara("combustivel")
        frm.ShowDialog()
        If (frm.codigo <> 0) Then
            'algum usuario foi clicado
            objBLL.localizar(frm.codigo)
            If (objBLL.pCodigo <> 0) Then
                carregaCampos()
                atual = objBLL.pCodigo
            Else
                MessageBox.Show("Registro Inexistente!")
                bVaPara_Click(sender, e)
            End If
        End If
        status(True)
        frm = Nothing
    End Sub


    Private Sub bSalvar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bSalvar.Click
        Try
            carregaPropriedades()
            If (controle = "I") Then
                objBLL.inserir()
                MessageBox.Show("Incluido com sucesso!")
                limpar()
                carregaPrimeiroUltimo()
                carregaUltimo()
                status(True)
            Else
                objBLL.alterar()
                MessageBox.Show("Alterado com sucesso!")

                status(True)
            End If

        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub


    Private Sub bSair_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bSair.Click
        Me.Close()
    End Sub

    Private Sub bUltimo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bUltimo.Click
        carregaUltimo()
        atual = ultimo
        status(True)
    End Sub

    Private Sub bPrimeiro_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bPrimeiro.Click
        carregaPrimeiro()
        atual = primeiro
        status(True)
    End Sub

    Private Sub bProximo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bProximo.Click
        Dim codigo = objBLL.localizarProxAnteriro("proximo", atual)
        objBLL.localizar(codigo)
        If (objBLL.pCodigo <> 0) Then
            carregaCampos()
            atual = objBLL.pCodigo
        End If
        status(True)
    End Sub

    Private Sub bAnterior_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bAnterior.Click
        Dim codigo = objBLL.localizarProxAnteriro("anterior", atual)
        objBLL.localizar(codigo)
        If (objBLL.pCodigo <> 0) Then
            carregaCampos()
            atual = objBLL.pCodigo
        End If
        status(True)
    End Sub


    Private Sub bPesquisar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bPesquisar.Click
        dgDados.DataSource = Nothing
        If (Me.Location.Y = 212) Then
            gbConsulta.Visible = True
            Me.Location = New System.Drawing.Point(Me.Location.X, Me.Location.Y - 127)
            Me.Size = New Size(734, 563)
        Else
            gbConsulta.Visible = False
            Me.Location = New System.Drawing.Point(Me.Location.X, Me.Location.Y + 127)
            Me.Size = New Size(734, 304)
        End If
    End Sub

    Private Sub bClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bClose.Click
        gbConsulta.Visible = False
        Me.Location = New System.Drawing.Point(Me.Location.X, Me.Location.Y + 127)
        Me.Size = New Size(734, 304)
    End Sub

    Private Sub bFiltrar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bFiltrar.Click
        atualizaGrid()
    End Sub

    Private Sub dgDados_CellDoubleClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgDados.CellDoubleClick
        Dim codigo As Integer
        If (e.RowIndex >= 0) Then
            codigo = dgDados.Rows(e.RowIndex).Cells("com_codigo").Value
            objBLL.localizar(codigo)
            If (objBLL.pCodigo <> 0) Then
                carregaCampos()
                atual = objBLL.pCodigo
                gbConsulta.Visible = False
                Me.Location = New System.Drawing.Point(Me.Location.X, Me.Location.Y + 127)
                Me.Size = New Size(734, 304)
            End If
        End If
    End Sub


End Class